package com.yaosheng.thread.thread;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by yaosheng on 2021/7/2.
 * 虚引用:管理堆外内存
 */
public class TestPhantomReference {

    private static final List<Object> LIST = new LinkedList<> ();
    private static final ReferenceQueue QUEUE = new ReferenceQueue ();

    public static void main(String[] args) {

        PhantomReference<M> phantomReference = new PhantomReference<M> (new M (),QUEUE);

        new Thread ( () -> {
            while(true){
                LIST.add (new byte[1024 * 1024]);
                try{
                    Thread.sleep (1000);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                System.out.println (phantomReference.get ());
            }
        }).start ();

        new Thread ( () -> {
            while (true){
                Reference< ? extends M> poll =QUEUE.poll ();
                if(poll != null){
                    System.out.println (" ---虚引用对象被JVM回收了--- " + poll);
                }
            }
        }).start ();
    }

    static class M {
        String name = "yaosheng";
    }
}
